package com.lzh.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzh.common.utils.AppJwtUtil;
import com.lzh.common.utils.Md5Util;
import com.lzh.media.entity.WmUser;
import com.lzh.media.mapper.WmUserMapper;
import com.lzh.media.service.WmUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 自媒体用户信息表 服务实现类
 * </p>
 *
 * @author lzh
 * @since 2021-06-24
 */
@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements WmUserService {

    @Resource
    private WmUserMapper wmUserMapper;

    @Override
    public Map<String, Object> login(WmUser wmUser) {
        QueryWrapper<WmUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",wmUser.getName());
        WmUser user = wmUserMapper.selectOne(queryWrapper);
        String pwd = Md5Util.toMD5(wmUser.getPassword(), user.getSalt());
        if (pwd.equals(user.getPassword())) {
        //wm 用户登录成功，颁发令牌
            user.setPassword("****");
            user.setSalt("");
            HashMap<String, Object> userMap = new HashMap<>();
            userMap.put("user",user);
            String token = AppJwtUtil.createToken(user.getApUserId().longValue());
            userMap.put("token",token);
            return userMap;
        }
        return null;
    }
}
